package com.fosung.frame.modules.sys.utils;

import java.io.BufferedInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.google.gson.Gson;

import net.sf.json.JSONObject;


public class HttpURLConnectionUtil {

	 
	 /**
	     * 从网络获取json数据,(String byte[})

	     * @param path
	     * @return
	     */
	    public static String getJsonByInternet(String path){
	        try {
	            URL url = new URL(path.trim());
	            //打开连接
	            HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();

	            if(200 == urlConnection.getResponseCode()){
	                //得到输入流
	                InputStream is =urlConnection.getInputStream();
	                ByteArrayOutputStream baos = new ByteArrayOutputStream();
	                byte[] buffer = new byte[1024];
	                int len = 0;
	                while(-1 != (len = is.read(buffer))){
	                    baos.write(buffer,0,len);
	                    baos.flush();
	                }
	                return baos.toString("utf-8");
	            }
	        }  catch (IOException e) {
	            e.printStackTrace();
	        }

	        return null;
	    }


	 
	//获取其他页面的数据
	    /**
	     * POST请求获取数据
	     */
	    public static String postDownloadJson(String path,String post){
	        URL url = null;
	        try {
	            url = new URL(path);
	            HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
	            httpURLConnection.setRequestMethod("POST");// 提交模式
	            // conn.setConnectTimeout(10000);//连接超时 单位毫秒
	            // conn.setReadTimeout(2000);//读取超时 单位毫秒
	            // 发送POST请求必须设置如下两行
	            httpURLConnection.setDoOutput(true);
	            httpURLConnection.setDoInput(true);
	            httpURLConnection.setRequestProperty("Content-Type", "application/json"); 
	            // 获取URLConnection对象对应的输出流
	            PrintWriter printWriter = new PrintWriter(httpURLConnection.getOutputStream());
	            // 发送请求参数
	            printWriter.write(post);//post的参数 xx=xx&yy=yy
	            // flush输出流的缓冲
	            printWriter.flush();
	            //开始获取数据
	            BufferedInputStream bis = new BufferedInputStream(httpURLConnection.getInputStream());
	            ByteArrayOutputStream bos = new ByteArrayOutputStream();
	            int len;
	            byte[] arr = new byte[1024];
	            while((len=bis.read(arr))!= -1){
	                bos.write(arr,0,len);
	                bos.flush();
	            }
	            bos.close();
	            return bos.toString("utf-8");
	        } catch (Exception e) {
	            e.printStackTrace();
	        }
	        return null;
	    }
	    public static String postJson(String path,String post){
	        URL url = null;
	        try {
	            url = new URL(path);
	            HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
	            httpURLConnection.setRequestMethod("POST");// 提交模式
	            // conn.setConnectTimeout(10000);//连接超时 单位毫秒
	            // conn.setReadTimeout(2000);//读取超时 单位毫秒
	            // 发送POST请求必须设置如下两行
	            httpURLConnection.setDoOutput(true);
	            httpURLConnection.setDoInput(true);
	            httpURLConnection.setRequestProperty("Authorization", "Bearer cGFydHktcGVvcGxlLWRjZXpiOmlkbzg1UiZEY2VudGVy"); 
	            httpURLConnection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded"); 
	            httpURLConnection.setRequestProperty("Charset", "UTF-8");
	            // 获取URLConnection对象对应的输出流
	            PrintWriter printWriter = new PrintWriter(httpURLConnection.getOutputStream());
	            // 发送请求参数
	            printWriter.write(post);//post的参数 xx=xx&yy=yy
	            // flush输出流的缓冲
	            printWriter.flush();
	            //开始获取数据
	            BufferedInputStream bis = new BufferedInputStream(httpURLConnection.getInputStream());
	            ByteArrayOutputStream bos = new ByteArrayOutputStream();
	            int len;
	            byte[] arr = new byte[1024];
	            while((len=bis.read(arr))!= -1){
	                bos.write(arr,0,len);
	                bos.flush();
	            }
	            bos.close();
	            return bos.toString("utf-8");
	        } catch (Exception e) {
	            e.printStackTrace();
	        }
	        return null;
	    }
	    
	@SuppressWarnings("unchecked")
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Map<String, Object> param = new HashMap<String, Object>();
//		param.put( "orgId", "29cf0ebb-a6ee-444d-abc8-741ca9220ba7");
		param.put( "id", "fa8e44e4-57f3-11e7-83c1-0050569a68e4");
		param.put( "manageOrgId", "");
		param.put( "domainType", "1");
		
//		String pos=postDownloadJson("http://10.1.1.194:7612/subordinate/query",JSONHelper.toJSONString(param));
//		Gson gson = new Gson();
//		List<Map<String, Object>> list2 = (List<Map<String, Object>>) gson.fromJson(pos, Object.class);
//		for (int i = 0; i < list2.size(); i++) {  
//            System.out.println((String) list2.get(i).get("id"));  
//        }  
//		String pos=postDownloadJson("http://10.1.1.193:7712/imis/queryMemberListByOrgId",JSONHelper.toJSONString(param));
//		String pos1=getJsonByInternet("http://10.243.32.7:8962/simpledata/queryUserByOrgId/"
//						+"374ac19c-139c-49ed-8354-bd14dcd03083");
//		JSONObject  jasonObject = JSONObject.fromObject(pos1);
//		Map<String,List<Map<String, Object>>> map = (Map)jasonObject;
//		List<Map<String, Object>> list2 =map.get("users");
//		for (int i = 0; i < list2.size(); i++) {  
//	      System.out.println((String) list2.get(i).get("orgId"));  
//	      System.out.println((String) list2.get(i).get("userName"));  
//	  } 
//		String url="http://10.254.23.58:8962/simpledata/queryUserInfoByHash";
//		String para="VedKtE2blLU3Px8/ypfDIB65Vps=";
//        JSONArray array = new JSONArray();
//        array.add(para);
//        String pos=postDownloadJson(url,array.toString());
//        JSONArray jsArr = JSONArray.fromObject(pos.toString());  
//        Map map=(Map) jsArr.opt(0);
//        System.out.println(pos);
//        System.out.println(map.get("orgId"));
		 
		String pos1=getJsonByInternet("http://10.254.23.41:8962/simpledata/queryOrgInfo/e011c4a8-57f1-11e7-a1cc-0050569a68e4");
		Gson gson = new Gson();
		List<Map<String, Object>> list2 = (List<Map<String, Object>>) gson.fromJson(pos1, Object.class);
		String pos2=getJsonByInternet("http://10.254.23.41:8962/simpledata/getOrgById/e011c4a8-57f1-11e7-a1cc-0050569a68e4");
		JSONObject  jasonObject = JSONObject.fromObject(pos2);
		Map<String,Object> map = (Map<String, Object>)jasonObject;
		System.out.println(pos2);
		System.out.println(pos1);
	}

}
